package com.example.myfinalhomework;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;

public class TXmanager {
    private static DBHelper dBhelper;
    private static String TBNAME;

    public TXmanager(Context context) {
        dBhelper = new DBHelper(context);
        TBNAME = DBHelper.TB_NAME;
    }

    public static void add(String name, String num, String remark) {
        SQLiteDatabase db = dBhelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("num", num);
        values.put("remark", remark);
        db.insert(TBNAME, null, values);
        db.close();
    }

    @SuppressLint("Range")
    public static List<userrelated> listAll() {
        List<userrelated> list = new ArrayList<>();
        SQLiteDatabase db = dBhelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            userrelated item = new userrelated();
            item.setName(cursor.getString(cursor.getColumnIndex("name")));
            item.setNum(cursor.getString(cursor.getColumnIndex("num")));
            item.setRemark(cursor.getString(cursor.getColumnIndex("remark")));
            list.add(item);
        }
        cursor.close();
        db.close();
        return list;
    }

    public static void delete(String name) {
        SQLiteDatabase db = dBhelper.getWritableDatabase();
        db.delete(TBNAME, "name = ?", new String[]{name});
        db.close();
    }

    public static void update(String name, String num, String remark) {
        SQLiteDatabase db = dBhelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("num", num);
        values.put("remark", remark);
        db.update(DBHelper.TB_NAME, values, null, null);
        db.close();
    }

    @SuppressLint("Range")
    public static userrelated getByName(String name) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = dBhelper.getReadableDatabase();
            cursor = db.query(TBNAME, null, "name = ?", new String[]{name}, null, null, null);
            userrelated item = null;
            if (cursor.moveToFirst()) {
                item = new userrelated();
                item.setName(cursor.getString(cursor.getColumnIndex("name")));
                item.setNum(cursor.getString(cursor.getColumnIndex("num")));
                item.setRemark(cursor.getString(cursor.getColumnIndex("remark")));
            }
            return item;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null && db.isOpen()) {
                db.close();
            }
        }
    }
}